
package me.zhengjie.rest;

import me.zhengjie.annotation.Log;
import me.zhengjie.domain.AppEvent;
import me.zhengjie.service.AppEventService;
import me.zhengjie.service.dto.AppEventQueryCriteria;
import me.zhengjie.utils.SecurityUtils;
import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

/**
* @website https://el-admin.vip
* @author IQW
* @date 2022-07-03
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "事件上报管理")
@RequestMapping("/api/appEvent")
public class AppEventController {

    private final AppEventService appEventService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('appEvent:list')")
    public void exportAppEvent(HttpServletResponse response, AppEventQueryCriteria criteria) throws IOException {
        appEventService.download(appEventService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询事件")
    @ApiOperation("查询事件")
//    @PreAuthorize("@el.check('appEvent:list')")
    public ResponseEntity<Object> queryAppEvent(AppEventQueryCriteria criteria,
                                                @RequestParam(defaultValue = "0") int force,
                                                @PageableDefault(sort = {"eventId"}, direction = Sort.Direction.DESC) Pageable pageable){

        if(force == 0) {
            //是否强行查看关联事件
            criteria.setToUserId(SecurityUtils.getCurrentUserId().intValue());
        }
        if(null != criteria.getStatus() && criteria.getStatus() == -1){
            //后台首页查询（待处理）
            criteria.setStatusLessTh(1);
            criteria.setStatus(null);
        }
        criteria.setFid(0); //不看回复的
        return new ResponseEntity<>(appEventService.queryAll(criteria,pageable),HttpStatus.OK);
    }



    @PostMapping
    @Log("新增事件上报")
    @ApiOperation("新增事件上报")
    @PreAuthorize("@el.check('appEvent:add')")
    public ResponseEntity<Object> createAppEvent(@Validated @RequestBody AppEvent resources){
        return new ResponseEntity<>(appEventService.create(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改事件上报")
    @ApiOperation("修改事件上报")
    @PreAuthorize("@el.check('appEvent:edit')")
    public ResponseEntity<Object> updateAppEvent(@Validated @RequestBody AppEvent resources){
        appEventService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @PutMapping(value = "/reply")
    @Log("修改事件上报-回复")
    @ApiOperation("修改事件上报-回复")
//    @PreAuthorize("@el.check('appEvent:edit')")
    public ResponseEntity<Object> replyAppEvent(@Validated @RequestBody AppEvent resources){
        appEventService.reply(resources, SecurityUtils.getCurrentUserId().intValue());
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除事件上报")
    @ApiOperation("删除事件上报")
    @PreAuthorize("@el.check('appEvent:del')")
    public ResponseEntity<Object> deleteAppEvent(@RequestBody Integer[] ids) {
        appEventService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}